<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/20
 * Time: 11:03
 */

class customService extends MY_Controller
{
    public function __construct()
    {

        parent::__construct();
        parent::checkLogin();

    }

    // 玩家或代理信息查询
    public function searchInfo()
    {
        // $gid = $this->input->post('gid');
        $uid = $this->input->post('uid');
        $type = $this->input->post('type');
        $result = array();

        if ($type == 1) {
            $DB2 = $this->load->database('account', true);
            $array = array('UserID' => $uid);
//        $array = array("invalid" => 0);
            $result = $DB2
                ->select('UserID,Accounts,RoomCard')
                ->where($array)
                ->get('AccountsInfo')->result_array();
        } else {
            $array = array(
                'id' => $uid,
            );
            $result = $this->db
                ->select('id,nickname,cardcount,phone')
                ->where($array)->get('Record_Member')->result_array();
        }
        if ($result) {
            $result[0]['status'] = 1;
            echo json_encode($result[0]);
        } else {
            echo json_encode(array('status' => '0'));
        }
    }

//玩家充值页面
    public function CardByCustom()
    {

        $type = $this->uri->segment(3);
        $action = $this->uri->segment(4);
        $result = array();
        $data['data'] = array($result, $_SESSION);
        $this->load->view('head.html', $data);
        if ($type == 'user' & $action == 'in') {
            $this->load->view('usercardadd.html');
        } else if ($type == 'user' & $action == 'out') {
            $this->load->view('usercardminus.html');
        } else if ($type == 'agent' & $action == 'in') {
            $this->load->view('agentcardadd.html');
        } else {
            $this->load->view('agentcardminus.html');
        }
        $this->load->view('footer.html');
    }

    public function doCardAction()
    {
        $type = $this->uri->segment(3);
        $action = $this->uri->segment(4);
        $gid = $this->input->post('gid');
        $uid = $this->input->post('uid');
        $cardNumber = $this->input->post('cardnumber');


        if ($action == "out") {
            $count = -$cardNumber;
        } else {
            $count = $cardNumber;
        }

        switch ($type) {
            case 'user':

                $DB2 = $this->load->database('account', true);
                $array = array('UserID' => $uid);
                $ret = $DB2
                    ->select('RoomCard as cardNumberBefore,NickName as nickname,Accounts as account')
                    ->where($array)
                    ->get('AccountsInfo')->result_array();
                if (!$ret) {
                    $this->alert_message(0, '玩家不存在,请先注册', site_url('user/agentLevelList'));
                    exit;
                }

                $data = array(
                    'RoomCard' => $ret[0]['cardNumberBefore'] + ($count),
                );
                $ret1 = $DB2->where('UserID', $uid)->update('AccountsInfo', $data);
                break;
            case 'agent':
                $array = array(
                    'id' => $uid,
                );
                $ret = $this->db
                    ->select('cardcount as cardNumberBefore,nickname,phone as account')
                    ->where($array)->get('Record_Member')->result_array();
                if (!$ret) {
                    $this->alert_message(0, '代理不存在,请先添加', site_url('user/agentLevelList'));
                    exit;
                }
                $data = array(
                    'cardcount' => $ret[0]['cardNumberBefore'] + ($count),
                );
                $ret1 = $this->db->where('id', $uid)->update('Record_Member', $data);
                break;
        }
        $data = array(
            'gid' => $gid,
            'type' => $this->getCustomCardType($type, $action),
            'cardnumber' => $cardNumber,
            'state' => '0',
            'uaid' => $uid,
            'aid' => $_SESSION['adminId'],
            'time' => time(),
            'nickname' => $ret[0]['nickname'],
            'cardbefore' => $ret[0]['cardNumberBefore'],
            'account' => $ret[0]['account'],
        );
        $ret = $this->db->insert('sys_custom_card', $data);

        if ($ret && $ret1) {
            if ($type == 'user') {
                $this->alert_message(1, '操作成功', site_url('customService/customCardList'));
            } else {
                $this->alert_message(1, '操作成功', site_url('customService/customCardList'));
            }
        } else {
            $this->alert_message(0, '操作失败', site_url('customService/customCardList'));
        }
    }

//代充值、退卡列表
    function customCardList()
    {
        $array = array();
//        $array = array("invalid" => 0);
        $result = $this->db
            ->select('gid,type,account,time,aid,uaid,cardnumber,nickname,cardbefore')
            ->where($array)
            ->get('sys_custom_card')->result_array();


        $data['data'] = array($result, $_SESSION);
        $this->load->view('head.html', $data);
        $this->load->view('customcardlist.html');
        $this->load->view('footer.html');

    }

//带充值退卡类型
    function getCustomCardType($type, $action)
    {
        $state = 0;
        switch ($type) {
            case "user":
                switch ($action) {
                    case "in":
                        $state = 0;
                        break;
                    case "out":
                        $state = 1;
                        break;
                }
                break;
            case "agent":
                switch ($action) {
                    case "in":
                        $state = 2;
                        break;
                    case "out":
                        $state = 3;
                        break;
                }
                break;
        }
        return $state;
    }

    //代理注册列表
    public function customRegList()
    {

        $array = array();
//        $array = array("invalid" => 0);
        $result = $this->db
            ->select('id,gid,nickname,phone,address,wechat,bankname,bankcard,realname,time,admin_rid,invalid,admin_pid')
            ->where($array)
            ->get('sys_custom_reg')->result_array();

        $data['data'] = array($result, $_SESSION);
        $this->load->view('head.html', $data);
        $this->load->view('customreglist.html');
        $this->load->view('footer.html');

    }

    //代理注册操作
    public function doCustomReg()
    {
        $phone = $this->input->post('phone');
        $array = array(
            'phone' => $phone,
        );
        $ret = $this->db->where($array)->get('Record_Member')->result_array();

        if ($ret) {
            $this->alert_message(0, '代理用户已经存在', site_url('customService/customRegList'));
        } else {
            $gid = $this->input->post('gid');
            $nicakname = $this->input->post('nickname');
            $pass = $this->input->post('pwd');
            $pass1 = $this->input->post('pwd1');
            $address = $this->input->post('address');
            $realname = $this->input->post('realname');
            $bankname = $this->input->post('bankname');
            $bankcard = $this->input->post('bankcard');
            $wechat = $this->input->post('wechat');

            if ($pass != $pass1) {
                $this->alert_message(0, '两次输入的密码不一致', site_url('customService/customRegList'));
            } else {
                $data = array(
                    'gid' => $gid,
                    'nickname' => $nicakname,
                    'pwd' => $pass,
                    'phone' => $phone,
                    'address' => $address,
                    'wechat' => $wechat,
                    'bankcard' => $bankcard,
                    'bankname' => $bankname,
                    'realname' => $realname,
                    'time' => time(),
                    'admin_rid' => $_SESSION['adminId']
                );
                $ret = $this->db->insert('sys_custom_reg', $data);
                if ($ret) {
                    $this->alert_message(1, '添加成功', site_url('customService/customRegList'));
                } else {
                    $this->alert_message(0, '添加失败', site_url('customService/customRegList'));
                }

            }
        }
    }

    //代理账户审批操作
    public function doCustomRegPass()
    {
        $id = $this->uri->segment(3);
        $state = $this->uri->segment(4);

        $data = array(
            'invalid' => $state,
            'admin_pid' => $_SESSION['adminId']
        );
        $ret = $this->db->where('id', $id)->update('sys_custom_reg', $data);
        if ($ret) {
            $this->alert_message(1, '添加成功', site_url('customService/customRegList'));
        } else {
            $this->alert_message(0, '添加失败', site_url('customService/customRegList'));
        }

    }

    public function customResetList()
    {
        $array = array();
//        $array = array("invalid" => 0);
        $result = $this->db
            ->select('id,gid,aid,newpwd,state,drcp,time')
            ->where($array)
            ->get('sys_custom_reset')->result_array();

        $result = $this->db
            ->select('aa.id,aa.gid,aa.time,aa.drcp,aa.newpwd,aa.aid,aa.admin_id,bb.nickname,bb.phone')
            ->from('sys_custom_reset aa')
            ->join('Record_Member bb', 'aa.aid = bb.id', 'left')
            ->where($array)
            ->get()
            ->result_array();
        $data['data'] = array($result, $_SESSION);
        $this->load->view('head.html', $data);
        $this->load->view('customresetlist.html');
        $this->load->view('footer.html');

    }
//密码修改
    public function doCustomReset()
    {

        $gid = $this->input->post('gid');
        $uid = $this->input->post('uid');
        $drcp = $this->input->post('drcp');


        $array = array(
            'id' => $uid,
        );
        $ret = $this->db
            ->where($array)->get('Record_Member')->result_array();
        if (!$ret) {
            $this->alert_message(0, '代理不存在,请先添加', site_url('user/agentLevelList'));
            exit;
        }

        $pwd=rand(100000, 999999);
        $data = array(
            'gid' => $gid,
            'admin_id' => $_SESSION['adminId'],
            'time' => time(),
            'aid' =>$uid,
            'drcp' => $drcp,
            'newpwd' => $pwd
        );
        $this->load->library('Sms.php');
        $phone=$ret[0]['phone'];
        $datas = array(
            $pwd
        );
//      $tempId=1;
        //具体的短信发送模板
        $tempId = 197068;
        $this->sms->sendTemplateSMS($phone, $datas, $tempId);
        $ret = $this->db->insert('sys_custom_reset', $data);

        $arr = array(
            'pwd' => md5($pwd)
        );
        $ret1 = $this->db->where('id', $uid)->update('Record_Member', $arr);

        if ($ret&&$ret1) {
            $this->alert_message(1, '密码修改成功', site_url('customService/customResetList'));
        } else {
            $this->alert_message(0, '密码修改失败', site_url('customService/customResetList'));
        }

    }
}